# coding: utf-8
from sshtunnel import SSHTunnelForwarder

"""
CreateSSHServer module
create on 11/01/2018
"""

def server(dataInfo):
	"""创建ssh通道服务

	:param remote_ip_address: SSH远程中转服务器ip地址
	:param ssh_port: SSH远程服务器连接端口
	:param ssh_username: 远程服务器用户名
	:param ssh_passwd: 远程服务器密码
	:param remote_db_address: 数据库地址
	:param remote_db_port: 数据库端口号
	Usage:
		server = server(dataInfo)
		server.start()
		port = server.local_bind_port
		...MYSQL Connection...
		server.close()
	"""
	remote_ip_address 	= dataInfo.get('remote_ip_address', '')
	ssh_port 			= dataInfo.get('ssh_port', 22)
	ssh_username 		= dataInfo.get('ssh_username', '')
	ssh_passwd 			= dataInfo.get('ssh_passwd', '')
	remote_db_address 	= dataInfo.get('remote_db_address', '')
	remote_db_port 		= dataInfo.get('remote_db_port', 3306)
	
	return SSHTunnelForwarder((remote_ip_address, int(ssh_port)),
							ssh_password=ssh_passwd,
							ssh_username=ssh_username,
							remote_bind_address=(remote_db_address, int(remote_db_port))
							)
